export default function({$axios, redirect, route, store}) {

  $axios.onRequest(config => {
    // 也可以从cookie中读取token，但是拦截器调用是个频繁操作，cookie读取就是读磁盘，vuex读取是读内存
    config.headers.token = store.state.user.token
    return config
  })

  $axios.onResponse(response => {
    if(response.data.code === 200) {
      return response.data
    } else if(response.data.code === 401 && route.fullPath !== '/login'){
      // 登录失败
      store.commit('user/M_UPDATE_USER', response.data.result)
      redirect('/login?path='+route.fullPath)
    }
  })

  $axios.onError(error => {
    return error
  })
}